<template>
	<div class="cms-channel-container">
		<el-dialog title="修改栏目" v-model="isShowDialog" >
			<el-form ref="addChannelFormRef" :model="formData" :rules="formRules" label-width="80px">
				<el-row :gutter="35">
					<el-col>
						<el-form-item label="上级栏目" prop="parentId">
							<el-cascader
								placeholder="请选择上级栏目"
								v-model="formData.parentId"
								:options="channels"
								:props="{expandTrigger: 'hover', emitPath: false, checkStrictly: true, value: 'id', label: 'name', children: 'children' }"
								class="w100"
								clearable>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="栏目名称" prop="name">
							<el-input v-model="formData.name" placeholder="请输入栏目名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="栏目类型" prop="type">
							<el-select v-model="formData.type" placeholder="请选择栏目类型" clearable class="w100">
								<el-option 
									v-for="item in channelTypes"
									:key="item.dictValue"
									:disabled="item.dictValue === '1'"
									:label="item.dictName"
									:value="item.dictValue">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="栏目状态" prop="status">
							<el-select v-model="formData.status" placeholder="请选择栏目状态" clearable class="w100">
								<el-option label="正常" value="0"></el-option>
								<el-option label="停用" value="1"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="栏目排序" prop="orderNum">
							<el-input-number v-model="formData.orderNum" :min="0" :max="999" clearable class="w100"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col>
						<el-form-item label="备注" prop="remark">
							<el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel">取 消</el-button>
					<el-button type="primary" @click="onSubmit">保 存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { lineToTreeData } from '/@/utils/commonFunction';
import { getDictionaryByLabel } from '/@/api/system/dictionary/index';
import { getChannelList, editChannel } from '/@/api/cms/channel/index';
export default {
	name: 'cms/channel/add',
	setup(props, context) {
		const addChannelFormRef = ref();
		const state = reactive({
			isShowDialog: false,
			channels: [],
			channelTypes: [],
			formData: {
				orderNum: 1
			},
			formRules : {
				parentId : { required: true, message: '上级栏目不能为空', trigger: 'change' },
				name: { required: true, message: '栏目名称不能为空', trigger: 'blur' },
				status: { required: true, message: '栏目状态不能为空', trigger: 'change' },
			}
		});
		
		//初始化上级栏目级联
		const loadChannels = async () => {
			await getChannelList({siteId: state.formData.siteId}).then((res) => {
				if(res.code == 0){
					state.channels = lineToTreeData(res.data);
				}
			});
		}
		
		const loadChannelTypes = () => {
			getDictionaryByLabel({dictLabel: 'cms_channel_type'}).then((res: any) => {
				if(res.code == 0){
					state.channelTypes = res.data;
				}
			});
		}

		// 打开弹窗
		const openDialog = (row?: object) => {
			if(row){
				state.isShowDialog = true;
				state.formData = Object.assign({}, row);
			}
			loadChannels();
			loadChannelTypes();
		};
		// 关闭弹窗
		const closeDialog = (row?: object) => {
			state.isShowDialog = false;
			for(var key in state.formData) {
				state.formData[key] = undefined;
			}
			addChannelFormRef.value.resetFields();
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = () => {
			addChannelFormRef.value.validate(valid => {
				if(valid){
					let params = {
						id: state.formData.id,
						siteId: state.formData.siteId,
						parentId: state.formData.parentId,
						type: state.formData.type,
						name: state.formData.name,
						status: state.formData.status,
						orderNum: state.formData.orderNum,
						remark: state.formData.remark
					}
					editChannel(params).then((res) => {
						if(res.code === 0){
							ElMessage.success('保存成功');
							context.emit('submit');
							closeDialog();
						}
					});
				}
			});
		};
		return {
			addChannelFormRef,
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			...toRefs(state),
		};
	},
};
</script>